﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;
using Lamex.Library.CommonClass;

using System.Runtime.InteropServices;
using Lamex.Interface;
using Lamex.mSchedule.UI;
using Lamex.Common;

namespace Lamex.mMES.UI
{
    public partial class frmMES_Layout_Panel : frmBaseChild
    {
        [DllImport("user32.dll", EntryPoint = "AnimateWindow")]
        private static extern bool AnimateWindow(IntPtr handle, int ms, int flags);

        DataTable dtRGB = null;

        Bitmap imgFront;        //原始图片
        Bitmap imgBack;         //参考图片
        TextBox myText;         //显示信息
        string myInfo;

        public frmMES_Layout_Panel()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            //0x00000001;	从左到右显示
            //0x00000002;	从右到左显示
            //0x00000004;	从上到下显示
            //0x00000008;	从下到上显示
            //0x00000010;	若使用了AW_HIDE标志，则使窗口向内重叠，即收缩窗口；否则使窗口向外扩展，即展开窗口
            //0x00010000;	隐藏窗口，缺省则显示窗口
            //0x00020000;	激活窗口。在使用了AW_HIDE标志后不能使用这个标志
            //0x00040000;	使用滑动类型。缺省则为滚动动画类型。当使用AW_CENTER标志时，这个标志就被忽略
            //0x00080000;	透明度从高到低

            base.OnLoad(e);
            AnimateWindow(this.Handle, 1000, 0x00000001);   // 居中逐渐显示。
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            AnimateWindow(this.Handle, 1000, 0x00000001);   // 居中逐渐显示。
        }

        private void frmMES_Layout_Panel_Load(object sender, EventArgs e)
        {
            dtRGB = Lamex.Base.BaseBuildSql.GetDataTableBySql(" select * From MES_Layout_Block_Color");

            string strPath = @"\\Tx-fs10\tx-fs1\LAMEX\MES\SysSource\Image\Layout";
            int SW = Screen.PrimaryScreen.Bounds.Width;
            int SH = Screen.PrimaryScreen.Bounds.Height;

            try
            {
                imgFront = new Bitmap(strPath + "\\" + SW.ToString() + "x" + SH.ToString() + "_Panel_Front" + ".png");
                imgBack = new Bitmap(strPath + "\\" + SW.ToString() + "x" + SH.ToString() + "_Panel_Back" + ".png");

                pictureBox1.Tag = "Front IMG";
                pictureBox1.Image = imgFront;       //加载图像                   
                pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;

                myText = new TextBox();
                this.Controls.Add(myText);
                myText.Multiline = true;
                myText.Enabled = false;
                myText.Parent = pictureBox1;
                myText.Width = 120;
                myText.Height = 64;
                myText.Visible = true;
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
                MessageBox.Show(ex.Message + "\r\n" + "屏幕分辨率不正确！系统只支持以下分辨率：" + "\r\n"
                    + "1920x1080" + "\r\n"
                    + "1600x900" + "\r\n"
                    + "1366x768");
                return;
            }       
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                Color clr = imgBack.GetPixel(e.X + 10, e.Y + 10);         //获取在参考图对应坐标像素点的颜色信息
                myInfo = Lamex.Common.GetCheckRGBHelper.GetCheckRGB(clr, dtRGB);
                if (myInfo.Contains("无效区域"))
                {
                    pictureBox1.Cursor = Cursors.Arrow;         
                    myText.Text = "";
                    myText.Visible = false;
                }
                else
                {
                    pictureBox1.Cursor = Cursors.Hand;          //设置手形
                    myText.Text = myInfo;
                    myText.Visible = true;
                    set_hand_shape(e.X, e.Y, myInfo);
                }
            }
            catch
            {
            }
        }

        private void set_hand_shape(int X, int Y, string msg)
        {
            //设置label位置
            if (X + 20 + myText.Width >= pictureBox1.Width)
                myText.Left = X - 20 - myText.Width;
            else
                myText.Left = X + 20;
            if (Y + 20 + myText.Height >= pictureBox1.Height)
                myText.Top = Y - 20 - myText.Height;
            else
                myText.Top = Y + 20;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Color clr = imgBack.GetPixel(MousePosition.X, MousePosition.Y - 100);         //获取在参考图对应坐标像素点的颜色信息
            myInfo = Lamex.Common.GetCheckRGBHelper.GetCheckRGB(clr, dtRGB);
            if (myInfo.Contains("P05") || myInfo.Contains("P41"))
            {
                //MdiTools.OpenChildForm(this.MdiParent as Lamex.Interface.IMdiForm, typeof(frmMES_Workshop_LamexFollowup), sender as ToolStripMenuItem);
                Loginer.CurrentUser.FunctionId = "menuSCH_ScanFollowUp";
                MdiTools.OpenChildWebBrowse(this.MdiParent as IMdiForm, new FrmMES_CNCDataCollection("P05"), sender as ToolStripMenuItem);
            }
            else
            {
                MessageBox.Show("单击位置为无效区域！");
            }
        }
    }
}